﻿using UnityEngine;
using System.Collections;

public class Joystick : MonoBehaviour {
	public GameObject player;
   
	public bool isPress = false;
    private Transform button;
    public static float h = 0;
    public static float v = 0;

    void Awake() {
        button = transform.Find("Button");
    }

    void OnPress(bool isPress) {
        this.isPress = isPress;
        if (isPress == false) {
            button.localPosition = Vector3.zero;
            h = 0; v = 0;
        }
    }

    void Update() {

        if (isPress) {

            Vector2 touchPos = UICamera.lastTouchPosition;
            touchPos -= new Vector2(1130, 150);
            float distance = Vector2.Distance(Vector2.zero, touchPos);
            if (distance > 122) {
                touchPos = touchPos.normalized * 122;
                button.localPosition = touchPos;


            } else {
                button.localPosition = touchPos;
            }

            h = touchPos.x / 122;
            //v = touchPos.y / 122;
			if(h<-0.3){

				player.transform.Rotate(0,h*180*Time.deltaTime,0); 
			}
			if(h>0.3){

				player.transform.Rotate(0,h*180*Time.deltaTime,0); 
			}
        }




    }

}
